package com.ruoyi.base.service.impl;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.base.vo.DbField;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BasePropertyMapper;
import com.ruoyi.base.domain.BaseProperty;
import com.ruoyi.base.service.IBasePropertyService;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 * 设备属性Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-02-28
 */
@Service
public class BasePropertyServiceImpl implements IBasePropertyService 
{
    public static final String CACHE_MAP = "base_property";
    private static final Logger log = LoggerFactory.getLogger(BasePropertyServiceImpl.class);

    @Autowired
    private BasePropertyMapper basePropertyMapper;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private RedisCache redisCache;

    @PostConstruct
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 0;
    }


    /**
     * 查询设备表的属性字段
     *
     * @return 设备属性集合
     */
    @Override
    public List<DbField> selectDbPropertyList(){
        List<DbField> list = new ArrayList<>();
        try {
            Connection connection = dataSource.getConnection();
            DatabaseMetaData dm = connection.getMetaData();
            ResultSet rs = dm.getColumns(connection.getCatalog(), connection.getSchema(), "base_device", null);
            while (rs.next()) {
                String code = rs.getString("COLUMN_NAME");
                if(!code.toLowerCase().startsWith("prop_")){
                    continue;
                }
                DbField field = new DbField();
                field.setCode(code);
                field.setLabel(StringUtils.isEmpty(rs.getString("REMARKS")) ? rs.getString("COLUMN_NAME") : rs.getString("REMARKS"));
                field.setType(rs.getString("TYPE_NAME"));
                field.setRequired(rs.getInt("NULLABLE") == 1 ? "N" : "Y");
                list.add(field);
                log.debug(rs.getString("TABLE_NAME") + " - " + rs.getString("COLUMN_NAME") + " - " + rs.getString("COLUMN_NAME") + " - " + rs.getInt("NULLABLE"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 查询设备类型的属性
     *
     * @param typeId 设备类型主键
     * @return 设备属性
     */
    @Override
    public List<BaseProperty> selectBasePropertyByTypeId(Long typeId){
        String key = "type_"+typeId;
        List<BaseProperty> list = redisCache.getCacheMapValue(CACHE_MAP, key);
        if(list != null){
            return list;
        }

        BaseProperty pc = new BaseProperty();
        pc.setTypeId(typeId);
        list = basePropertyMapper.selectBasePropertyList(pc);
        redisCache.setCacheMapValue(CACHE_MAP, key, list);
        return list;
    }

    /**
     * 查询设备属性
     * 
     * @param id 设备属性主键
     * @return 设备属性
     */
    @Override
    public BaseProperty selectBasePropertyById(Long id)
    {
        return basePropertyMapper.selectBasePropertyById(id);
    }

    /**
     * 查询设备属性列表
     * 
     * @param baseProperty 设备属性
     * @return 设备属性
     */
    @Override
    public List<BaseProperty> selectBasePropertyList(BaseProperty baseProperty)
    {
        return basePropertyMapper.selectBasePropertyList(baseProperty);
    }

    /**
     * 新增设备属性
     * 
     * @param baseProperty 设备属性
     * @return 结果
     */
    @Override
    public int insertBaseProperty(BaseProperty baseProperty)
    {
        BaseProperty old = basePropertyMapper.selectBasePropertyByCode(baseProperty.getTypeId(), baseProperty.getCode());
        if(old != null){
            throw new ServiceException("字段已存在:"+baseProperty.getCode());
        }
        return basePropertyMapper.insertBaseProperty(baseProperty) + reCache();
    }

    /**
     * 修改设备属性
     * 
     * @param baseProperty 设备属性
     * @return 结果
     */
    @Override
    public int updateBaseProperty(BaseProperty baseProperty)
    {
        BaseProperty old = basePropertyMapper.selectBasePropertyByCode(baseProperty.getTypeId(), baseProperty.getCode());
        if(old != null && !old.getId().equals(baseProperty.getId())){
            throw new ServiceException("字段已存在:"+baseProperty.getCode());
        }
        return basePropertyMapper.updateBaseProperty(baseProperty) + reCache();
    }

    /**
     * 批量删除设备属性
     * 
     * @param ids 需要删除的设备属性主键
     * @return 结果
     */
    @Override
    public int deleteBasePropertyByIds(Long[] ids)
    {
        return basePropertyMapper.deleteBasePropertyByIds(ids) + reCache();
    }

    /**
     * 删除设备属性信息
     * 
     * @param id 设备属性主键
     * @return 结果
     */
    @Override
    public int deleteBasePropertyById(Long id)
    {
        return basePropertyMapper.deleteBasePropertyById(id) + reCache();
    }


}
